<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */

/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

/**
 * 获取列表总行数
 * @param  string  $category 分类ID
 * @param  integer $status   数据状态
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_list_count($category, $status = 1){
    static $count;
    if(!isset($count[$category])){
        $count[$category] = D('Document')->listCount($category, $status);
    }
    return $count[$category];
}

/**
 * 获取段落总数
 * @param  string $id 文档ID
 * @return integer    段落总数
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_part_count($id){
    static $count;
    if(!isset($count[$id])){
        $count[$id] = D('Document')->partCount($id);
    }
    return $count[$id];
}

/**
 * 获取导航URL
 * @param  string $url 导航URL
 * @return string      解析或的url
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_nav_url($url){
    switch ($url) {
        case 'http://' === substr($url, 0, 7):
        case '#' === substr($url, 0, 1):
            break;        
        default:
            $url = U($url);
            break;
    }
    return $url;
}

function parse_goods_img($str , $len){
    $arr = explode(',' , $str );
    $tmp = array();
    for($i = 0 ; $i < $len ; $i ++){
        $tmp [] = $arr [$i];
    }
    if($len == 1){
        return $tmp [0];
    }
    return $tmp ; 
}


//获取推荐的商品
function  get_recommend_goods($limit = 3 ){
    $sql = "SELECT `goods_id` ,`goods_name` , `shop_price` , `market_price` , `original_img` FROM `zw_goods` WHERE is_recommend = 1 ORDER BY sort desc , goods_id desc LIMIT 0," . $limit ;
    $list = S( 'sql_' . md5($sql)) ; 
    if(!$list){
        $list = M()->query($sql);
        S('sql_' . md5($sql) , $list , 300) ; //推荐商品缓存5分钟 
    }
    return $list;
}

//获取热卖商品 
function get_hot_goods($limit = 7){
    
    $sql = "SELECT `goods_id` , `goods_name` , `shop_price` , `market_price` , `original_img` FROM `zw_goods` WHERE is_hot = 1 ORDER BY sort desc , goods_id desc LIMIT 0," . $limit ;
   $list = S( 'sql_' . md5($sql)) ; 

    if(!$list){
        $list = M()->query($sql);
        S('sql_' . md5($sql) , $list , 300) ; //推荐商品缓存5分钟 
    }
    return $list;

}

//获取商品详情 //缓存 . 
function get_goods_detail($gid){
    $sql = "SELECT * FROM zw_goods WHERE goods_id = '$gid'" ; 
    $list = S( 'sql_' . md5($sql)) ; 
    if(!$list){
        $list = M()->query($sql);
        $list = $list [0];
        S('sql_' . md5($sql) , $list , 300) ; //推荐商品缓存5分钟 
    }
    return $list;

}

//获取验证码
function get_rand_code($len = 6){
    $str = '';
    for($i = 0 ; $i < $len ; $i ++){
        $str .= rand(0,9);
    }
    return $str ;
}
//去除. 
function img_substr($str){
    return substr( $str ,1 , strlen($str));
}

//将手机号中间变*
function replacename($str){
    return preg_replace('/(\d{3})\d{4}(\d{3})/i', '$1****$2', $str);
}